package com.lj.back.manage.scaffold.module.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lj.back.manage.scaffold.common.response.R;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.common.valid.group.AddStrategy;
import com.lj.back.manage.scaffold.module.sys.DTO.SysResetUserDTO;
import com.lj.back.manage.scaffold.module.sys.DTO.SysUserDTO;
import com.lj.back.manage.scaffold.module.sys.DTO.SysUserPageQuery;
import com.lj.back.manage.scaffold.module.sys.DTO.SysUserUpdateDTO;
import com.lj.back.manage.scaffold.module.sys.VO.SysUserVO;
import com.lj.back.manage.scaffold.module.sys.annotation.SysLog;
import com.lj.back.manage.scaffold.module.sys.enums.OperateTypeEnum;
import com.lj.back.manage.scaffold.module.sys.service.SysUserService;
import com.lj.back.manage.scaffold.module.sys.util.SysCommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "后台管理---后台用户模块")
@ApiSupport(author = "lv")
@AllArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    @PostMapping("/add")
    @SysLog(value = "新增用户", operateType = OperateTypeEnum.INSERT)
    @ApiOperation(value = "新增用户")
    @ApiOperationSupport(order = 1, ignoreParameters = {"id", "status", "createTime"})
    @RequiresPermissions("sys:user:insert")
    public R addUser(@RequestBody @Validated(AddStrategy.class) SysUserDTO user){
        Long userId = sysUserService.addUser(user);
        return R.build("userId",userId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页查询用户列表")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:user:list")
    public R listUsers(@Validated SysUserPageQuery pageQuery){
        IPage<SysUserDTO> userPage = sysUserService.listUsers(pageQuery);
        IPage<SysUserVO> userVOIPage = CommonUtil.pageBeanConvert(userPage, SysUserVO.class);
        return R.success(userVOIPage);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询用户详情")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParam(name = "id", value = "用户ID", example = "1", required = true, dataTypeClass = Long.class)
    @RequiresPermissions("sys:user:select")
    public R getUser(@PathVariable long id){
        SysUserDTO userDTO = sysUserService.getUser(String.valueOf(id));
        SysUserVO sysUserVO = BeanUtil.copyProperties(userDTO, SysUserVO.class);
        return R.success(sysUserVO);
    }

    @GetMapping("/info")
    @ApiOperation(value = "查询用户详情")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:user:select")
    public R getUser(){
        SysUserDTO user = SysCommonUtil.getUser();
        SysUserVO sysUserVO = BeanUtil.copyProperties(user, SysUserVO.class);
        return R.success(sysUserVO);
    }

    @PatchMapping("/update")
    @SysLog(value = "修改用户基本信息", operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "修改用户基本信息")
    @ApiOperationSupport(order = 4)
    public R updateUser(@RequestBody @Validated SysUserUpdateDTO sysUserUpdateDTO){
        boolean isSuccess = sysUserService.updateUser(sysUserUpdateDTO);
        return R.build(isSuccess);
    }

    @DeleteMapping("/delete/{id}")
    @SysLog(value = "删除用户", operateType = OperateTypeEnum.DELETE)
    @ApiOperation(value = "删除用户")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "id", value = "用户ID", example = "1", required = true, dataTypeClass = Long.class)
    @RequiresPermissions("sys:user:delete")
    public R deleteUser(@PathVariable long id){
        boolean isSuccess = sysUserService.deleteUser(id);
        return R.build(isSuccess);
    }

    @PatchMapping("/reset")
    @SysLog(value = "重置用户信息", operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "重置用户信息")
    @ApiOperationSupport(order = 6)
    @RequiresPermissions("sys:user:update")
    public R resetUserInfo(@RequestBody @Validated SysResetUserDTO resetUser){
        boolean isSuccess = sysUserService.resetUserInfo(resetUser);
        return R.build(isSuccess);
    }
}
